home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / tpack / pasutils.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  8.9 KB  |  289 lines

  1. unit PasUtils;
  2. {------------------------------------------------------------------------------}
  3. { HODGEPODGE UTILITY LIBRARY                                                   }
  4. {------------------------------------------------------------------------------}
  5.  
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, ExtCtrls, Forms, TypInfo;
  11.  
  12. type
  13.  
  14.   TExceptionReAction = (reAsk, reRetry, reIgnore, reRaise);
  15.     {defines what Retry/ErrorMsg can do in response to an exception}
  16.  
  17.   TRealRecord = record
  18.     {utility type used to set reals to 0 or to check if they are.}
  19.     {this way from pascal mag, uses 5 instead of 18 bytes and fewer cycles.}
  20.     {set a real to zero:     TRealRecord(RealVar).Exponent:=0; }
  21.     {does a real equal zero: TRealRecord(RealVar).Exponent=0; }
  22.     Exponent: Byte;
  23.     Mantissa: Array[1..5] of Byte;
  24.     end;
  25.  
  26. {------------------------------------------------------------------------------}
  27. { UTILITY PROCEDURE DECLARATIONS                                               }
  28. {------------------------------------------------------------------------------}
  29.  
  30. Procedure CursorOff;                                     { Turn the cursor Off }
  31. Procedure CursorOn;                                      { Turn the Cursor On }
  32.  
  33. function TrailingChar(const Value:String;Trailer:Char):String; {insures a trailing character}
  34. function TrailingBackSlash(const Value:String):String;         {insures a trailing '\'}
  35.  
  36. procedure SplitString(const Input:String;SplitAt:Char; var Left,Right:String); {splits at char}
  37.  
  38. procedure LongintsLowHigh(var Low,High:LongInt);
  39.  
  40. function Max(i,j:longint):longint;
  41. function Min(i,j:longint):longint;
  42.  
  43. function ExpXY(x,y:extended):extended;
  44.  
  45. function FormatNumber(l:LongInt): String;
  46.  
  47.  
  48. function FormatCurrency(value:real):string;
  49.  
  50. function StripString(Input:String;StripChar:Char):String;
  51.  
  52. function Spaces(n:byte):string;
  53.  
  54. function GetEnumString(TypeInfo:PTypeInfo;Ordinal:longint):String;
  55.  
  56. function MakePChar(const Value:String):PChar;
  57. procedure MovePChar2PString(Dest:PString;Source:PChar;aFree:Boolean);
  58. procedure FreePChar(Value:PChar);
  59. function ReceivePChar(Value:PChar):String;
  60.  
  61. function LeftPadZero(const Value:String; Length:byte):string;
  62.  
  63.  
  64. const
  65.   BoolString:array[false..true] of string[5]=('FALSE','TRUE');
  66.  
  67.  
  68. {------------------------------------------------------------------------------}
  69. { PASCAL UTILITY IMPLEMENTATION                                                }
  70. {------------------------------------------------------------------------------}
  71. implementation
  72.  
  73. uses
  74.   WinProcs
  75.   ,SysUtils;
  76.  
  77. {------------------------------------------------------------------------------}
  78. { CURSOR ON/OFF                                                                }
  79. {------------------------------------------------------------------------------}
  80.  
  81. Procedure CursorOff;                                       { Turn the Cursor Off }
  82. Var
  83.   Cstate : Integer;                                        { Current cursor State }
  84. Begin
  85.   Cstate := ShowCursor(True);                              { Get State }
  86.   While Cstate >= 0 do Cstate := ShowCursor(False);        { While ON turn Off }
  87. End;
  88.  
  89. Procedure CursorOn;                                        { Turn Cursor On }
  90. Var
  91.   Cstate : Integer;                                        { Current cursor State }
  92. Begin
  93.   Cstate := ShowCursor(True);                              { Get current State }
  94.   While Cstate < 0 do Cstate := ShowCursor(True);          { While off turn on }
  95. End;
  96.  
  97. {------------------------------------------------------------------------------}
  98. { TRAILING CHARACTER, TRAILING BACKSLASH                                       }
  99. {------------------------------------------------------------------------------}
  100.  
  101. function TrailingChar(const Value:String;Trailer:Char):String; {insures a trailing character}
  102. begin
  103.   Result:=Value;
  104.   if copy(Value,length(Value),1)<>Trailer then
  105.     Result:=Result+Trailer;
  106. end;
  107.  
  108. function TrailingBackSlash(const Value:String):String; {insures a trailing '\'}
  109. begin
  110.   Result:=TrailingChar(Value,'\');
  111. end;
  112.  
  113. {------------------------------------------------------------------------------}
  114. { SPLIT STRING AT CHARACTER                                                    }
  115. {------------------------------------------------------------------------------}
  116.  
  117. procedure SplitString(const Input:String;SplitAt:Char; var Left,Right:String);
  118. {splits 'input' at 'splitchar' into 'left' and 'right' parts}
  119. var n:integer;
  120. begin
  121. n:=pos(SplitAt,Input);
  122. if n=0 then begin
  123.   left:=Input;
  124.   Right:='';
  125.   end
  126. else begin
  127.   Left:=Copy(Input,1,n-1);
  128.   Right:=Copy(Input,n+1,length(Input)-n);
  129.   end;
  130. end;
  131.  
  132. {---------------------------------------------------------------------------}
  133.  
  134. function StripString(Input:String;StripChar:Char):String;
  135. {removes 'StripChar' from 'Input'}
  136. var n:integer;
  137. begin
  138.   n:=pos(StripChar,Input);
  139.   while n>0 do begin
  140.     Input:=Copy(Input,1,n-1)+Copy(Input,n+1,length(Input)-n);
  141.     n:=pos(StripChar,Input);
  142.   end;
  143.   Result:=Input;
  144. end;
  145.  
  146. {------------------------------------------------------------------------------}
  147. { SWAP LONGINTS FOR PROPER HIGH/LOW                                            }
  148. {------------------------------------------------------------------------------}
  149.  
  150. procedure LongintsLowHigh(var Low,High:LongInt);
  151. var
  152.   i:longint;
  153. begin
  154.   if Low>High then begin
  155.     i:=low;
  156.     Low:=High;
  157.     High:=i;
  158.     end;
  159. end;
  160.  
  161. {------------------------------------------------------------------------------}
  162. { GET HIGH/LOW                                                                 }
  163. {------------------------------------------------------------------------------}
  164.  
  165. function Max(i,j:longint):longint;
  166. begin
  167.   if i>j then
  168.     Result:=i
  169.   else
  170.     Result:=j;
  171. end;
  172.  
  173. function Min(i,j:longint):longint;
  174. begin
  175.   if i<j then
  176.     Result:=i
  177.   else
  178.     Result:=j;
  179. end;
  180.  
  181. {------------------------------------------------------------------------------}
  182. { MATH FUNCTIONS                                                               }
  183. {------------------------------------------------------------------------------}
  184.  
  185. function ExpXY(x,y:extended):extended;
  186. begin
  187.   result:=Exp(y*ln(x));
  188. end;
  189.  
  190. {------------------------------------------------------------------------------}
  191. { STRING FORMATING FUNCTIONS                                                   }
  192. {------------------------------------------------------------------------------}
  193.  
  194. function FormatNumber(l:LongInt): String;
  195. begin
  196.   Result:= FormatFloat('###,###,###,##0.00',StrToFloat(IntToStr(l)));
  197. end;
  198.  
  199. function FormatCurrency(value:real):string;
  200. var
  201.   s, s2 :string;
  202.   n: integer;
  203.   minusflag : boolean;
  204. begin
  205.   minusflag:=(value<0);
  206.   s:=format('%.2f',[abs(value)]);
  207.   s2:=copy(s,length(s)-2,3);
  208.   s:=copy(s,1,length(s)-3);
  209.   n:=length(S);
  210.   while n>3 do
  211.   begin
  212.     s2:=','+copy(s,n-2,3)+s2;
  213.     n:=n-3;
  214.   end;
  215.   if n>0 then
  216.   begin
  217.     s2:=copy(s,1,n)+s2;
  218.   end;
  219.   if minusflag then
  220.     result:='$-'+s2
  221.   else
  222.     result:='$'+s2;
  223. end;
  224.  
  225. {------------------------------------------------------------------------------}
  226. {  ADDS ZEROS TO FRONT OF STRING                                               }
  227. {------------------------------------------------------------------------------}
  228.  
  229. function LeftPadZero(const Value:String; Length:byte):string;
  230. begin
  231.   Result:=Value;
  232.   while ord(Result[0]) < Length do
  233.     Result:='0'+Value;
  234. end;
  235.  
  236.  
  237. {------------------------------------------------------------------------------}
  238. {  RETURNS N SPACES                                                            }
  239. {------------------------------------------------------------------------------}
  240.  
  241. function spaces(n:byte):string;
  242. begin
  243.   Result:='';
  244.   while n>0 do begin
  245.     dec(n);
  246.     Result:=Result+' ';
  247.     end;
  248. end;
  249.  
  250. {------------------------------------------------------------------------------}
  251. {  TYPEINFO HOW TO REMINDER PROC                                               }
  252. {------------------------------------------------------------------------------}
  253.  
  254. function GetEnumString(TypeInfo:PTypeInfo;Ordinal:longint):String;
  255. begin
  256.   Result:=GetEnumName(TypeInfo,Ordinal)^;
  257. end;
  258.  
  259. {------------------------------------------------------------------------------}
  260. {  PCHAR AND PSTRING UTILITIES                                                 }
  261. {------------------------------------------------------------------------------}
  262.  
  263. function MakePChar(const Value:String):PChar;
  264. begin
  265.   GetMem(Result,256);           {make room for a pascal maxlen pchar}
  266.   StrPCopy(Result,Value);       {copy string passed into buffer}
  267. end;
  268.  
  269. procedure FreePChar(Value:PChar);
  270. begin
  271.   FreeMem(Value,256);
  272. end;
  273.  
  274. function ReceivePChar(Value:PChar):String;
  275. begin
  276.   Result:=StrPas(Value);
  277.   FreePChar(Value);
  278. end;
  279.  
  280. procedure MovePChar2PString(Dest:PString;Source:PChar;aFree:Boolean);
  281. begin
  282.   AssignStr(Dest,StrPas(Source));
  283.   if aFree then
  284.     FreePChar(Source);
  285. end;
  286.  
  287.  
  288. end.
  289.